<template>
  <div class="login-container">
    <!-- 头条模块 -->
    <van-nav-bar title="登录">
      <van-icon slot="left" name="arrow-left" @click="onBack" />
    </van-nav-bar>
    <!-- 表单模块 -->
    <van-form @submit="onSubmit" ref="userForm">
      <van-field
        name="mobile"
        v-model="user.mobile"
        placeholder="请输入手机号"
        :rules="userFormRules.mobile"
        type="number"
        maxlength="11"
      >
        <i slot="left-icon" class="toutiao toutiao-shouji"></i>
      </van-field>
      <van-field
        v-model="user.code"
        name="code"
        placeholder="请输入验证码"
        :rules="userFormRules.code"
        type="number"
        maxlength="6"
      >
        <i slot="left-icon" class="toutiao toutiao-yanzhengma"></i>
        <template #button>
          <van-count-down
            :time="60 * 1000"
            format="ss s"
            v-if="isCountDownShow"
            @finish="isCountDownShow = false"
          />
          <van-button
            size="small"
            native-type="button"
            round
            type="default"
            @click="onSendSms"
            v-else
          >
            <span slot="default" class="text">发送验证码</span>
          </van-button>
        </template>
      </van-field>
      <div style="margin: 16px;">
        <van-button block type="info" native-type="submit">提交</van-button>
        <p class="tips">万能验证码：246810</p>
      </div>
    </van-form>
  </div>
</template>

<script>
// 导入用户登录请求api模块
import { login, getSmsCode } from '@/api/Login.js'

export default {
  name: 'Login',
  data() {
    return {
      user: {
        mobile: '', // 手机号
        code: '' // 验证码
      },
      userFormRules: {
        // 用户名验证码正则校验规则
        mobile: [
          { required: true, message: '手机号不能为空' },
          { pattern: /^1[3|5|7|7]\d{9}$/, message: '请输入正确的手机号码' }
        ],
        code: [
          { required: true, message: '验证码不能为空' },
          { pattern: /^\d{6}$/, message: '请输入正确的验证码' }
        ]
      },
      isCountDownShow: false // 倒计时显示隐藏
    }
  },
  methods: {
    // 表单登录函数
    async onSubmit() {
      // 1.获取user
      const user = this.user

      // 2.表单验证
      this.$toast.loading({
        message: '加载中...', // 提示词
        forbidClick: true,
        duration: 0 // 持续时间 当前一直持续
      })

      // 3.提交表单请求登录

      try {
        // 登录成功
        const { data: res } = await login(user)

        this.$store.commit('setUser', res.data) // 将返回的token传入store仓库
        this.$toast.success('登录成功') // 当调用了success方法还有fail方法之后，duration持续时间关闭

        this.$router.push('/my')
      } catch (err) {
        // 登录失败
        if (err.response.status === 400) {
          this.$toast.fail('登录失败')
        } else {
          this.$toast.fail('登录失败')
        }
      }

      // 4.根据请求响应处理后续操作
    },
    // 发送验证码函数
    async onSendSms() {
      // 1.验证用户名是否通过
      try {
        await this.$refs.userForm.validate('mobile')
      } catch (err) {
        return // 返回不执行下列语句
      }

      // 2.通过执行下面请求
      this.isCountDownShow = true

      // 3.请求发送验证码
      try {
        await getSmsCode(this.user.mobile)
        this.$toast('发送成功')
      } catch (err) {
        this.isCountDownShow = false // 发送验证码失败时,计时器关闭
        if (err.response.status === 429) {
          this.$toast('发送太频繁了，请稍后重试')
        } else {
          this.$toast('发送失败，请稍后重试')
        }
      }
    },
    onBack() {
      this.$router.back()
    }
  }
}
</script>

<style lang="less" socped>
.login-container {
  .toutiao {
    font-size: 37px;
  }
  .van-button--small {
    height: 47px;
    width: 152px;
    color: #b6b6b6;
    line-height: 47px;
    text-align: center;
    padding: 0 5px;
    background-color: #ededed;
    box-sizing: content-box;
  }
  .van-button--block {
    border-radius: 10px;
    background-color: #6db4fb;
    letter-spacing: 2px;
    border: 0;
  }
  .text {
    font-size: 20px;
  }
  .tips {
    font-size: 30px;
  }
}
</style>
